﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using HomeDMSDomain;


namespace bind_n_find
{
    /// <summary>
    /// Enum defining logical conditions used in dialog 
    /// </summary>
    public enum and_or_e
    {
        and_t,
        or_t,
    }

    /// <summary>
    /// 'Filter changed' event arguments
    /// </summary>
    public class filter_ev_args : EventArgs
    {
        private string _tag;
        public string tag
        {
            get
            {
                return _tag;
            }
        }

        private string _description;
        public string description
        {
            get
            {
                return _description;
            }
        }

        private and_or_e _condition;
        public and_or_e condition
        {
            get
            {
                return _condition;
            }
        }


        public filter_ev_args(string tag, string description, and_or_e condition)
        {
            _tag = tag;
            _description = description;
            _condition = condition;
        }
    }
    public delegate void filter_ev_handler(object sender, filter_ev_args e);


    /// <summary>
    /// Interaction logic for find_ctrl.xaml
    /// </summary>
    public partial class find_ctrl : UserControl
    {
        public event filter_ev_handler filter_is_set;
        private BindnFindObjectsDataContext _currentContext;
        public BindnFindObjectsDataContext currentContext
        {
            set
            {
                _currentContext = value;
            }
        }
        public find_ctrl()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            _load_data();
        }

        private void _load_data()
        {
            this.comboBox1.Items.Clear();
            List<string> tags = new List<string>();
            if (_currentContext != null)
            {
                foreach (Tag tag in _currentContext.Tags)
                {
                    if (!tags.Contains(tag.Name))
                        tags.Add(tag.Name);

                }
            }
            comboBox1.DataContext = tags;
        }

        /// <summary>
        /// Set conditions and search pattern and fire 'filter_is_set' event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void find_button_Click(object sender, RoutedEventArgs e)
        {
            and_or_e condition=and_or_e.or_t;
            if ((bool) radioButton2.IsChecked)
            {
                condition=and_or_e.and_t;
            }
            string cb_value=null;
            if (comboBox1.SelectedValue != null)
            {
                cb_value = comboBox1.SelectedValue.ToString();
            }
            if (cb_value==null && string.IsNullOrEmpty(textBox1.Text))
                filter_is_set(sender, null);
            else
                filter_is_set(sender, new filter_ev_args(cb_value, textBox1.Text,condition));

        }

        /// <summary>
        /// Clear filter
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clear_button_Click(object sender, RoutedEventArgs e)
        {
            comboBox1.SelectedValue = null;
            textBox1.Text = string.Empty;
            radioButton1.IsChecked = true;
            filter_is_set(sender, null);

        }
    }
}
